package com.hbean.controller;

import com.hbean.entity.SysRole;
import com.hbean.entity.SysRoleMenu;
import com.hbean.service.SysRoleMenuService;
import com.hbean.service.SysRoleService;
import com.hbean.system.annotation.LogMethod;
import com.hbean.system.config.Base;
import com.hbean.system.config.SysCache;
import com.hbean.utils.HUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: null
 * @Project: hbean
 * @Author: hjibo
 * @CreateDate: 2018-10-27 21:53
 * @ModificationHistory:
 */

@Slf4j
@RestController
@RequestMapping("/sysRole")
public class SysRoleController extends Base {

    @Autowired
    private SysRoleService sysRoleService;
    @Autowired
    private SysRoleMenuService sysRoleMenuService;
    @Autowired
    private SysCache sysCache;




    /**
     * description: 查询
     * create_user: hjibo
     * create_date: 2018-09-29 10:13
     * param :
     */
    @RequestMapping("/getSysRoles")
    public List<SysRole> getSysRoles(HttpSession session){
        List<SysRole> sysRoles = null;
        try {
            sysRoles = sysRoleService.getDataForCache();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sysRoles;
    }

    /**
     * description: 查询
     * create_user: hjibo
     * create_date: 2018-09-29 10:13
     * param :
     */
    @RequestMapping("/getSysRole")
    public SysRole getSysRole(HttpServletRequest request){
        SysRole sysRole = null;
        try {
            List<SysRole> sysRoles = sysRoleService.getDataForCache();//从缓存中取数据
            sysRoles = sysRoles.stream().filter(sysRole1 -> sysRole1.getSr00().equals(request.getParameter("sr00"))).collect(Collectors.toList());
            sysRole = sysRoles.get(0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sysRole;
    }

    /**
     * description: 新增
     * create_user: hjibo
     * create_date: 2018-09-29 10:19
     * param :
     */
    @RequestMapping("/addSysRole")
    @LogMethod(logName = "新增角色")
    public Map addSysRole(HttpServletRequest request, HttpSession session){
        try {
            SysRole sysRole = new SysRole();

            if(HUtil.isNotEmpty(request.getParameter("sr01"))) sysRole.setSr01(request.getParameter("sr01"));
            if(HUtil.isNotEmpty(request.getParameter("sr02"))) sysRole.setSr02(request.getParameter("sr02"));
            if(HUtil.isNotEmpty(request.getParameter("sr04"))) sysRole.setSr04(request.getParameter("sr04"));
            if(HUtil.isNotEmpty(request.getParameter("sr05"))) sysRole.setSr05(request.getParameter("sr05"));
            if(HUtil.isNotEmpty(request.getParameter("sr10"))) sysRole.setSr10(Integer.parseInt(request.getParameter("sr10")));

            this.sysRoleService.insertDataAndUpdateCache(sysRole);

            return mapResult(SUCCESS_CODE,SUCCESS_MSG,LONG_0L,null);
        } catch (Exception e) {
            log.info("保存角色信息失败！");
            e.printStackTrace();
        }
        return mapResult(ERROR_CODE,ERROR_MSG,LONG_0L,null);
    }

    /**
     * description: 删除
     * create_user: hjibo
     * create_date: 2018-09-29 15:50
     * param :
     */
    @RequestMapping("/removeSysRole")
    @LogMethod(logName = "删除角色")
    public Map removeSysRole(HttpServletRequest request){
        try {
            SysRole sysRole = new SysRole();
            if(HUtil.isNotEmpty(request.getParameter("sr00"))){
                sysRole.setSr00(request.getParameter("sr00"));
                this.sysRoleService.deleteDataAndUpdateCache(sysRole);
                return mapResult(SUCCESS_CODE,SUCCESS_MSG,LONG_0L,null);
            }
        } catch (Exception e) {
            log.info("删除角色信息失败！");
            e.printStackTrace();
        }
        return mapResult(ERROR_CODE,ERROR_MSG,LONG_0L,null);
    }

    /**
     * description: 修改
     * create_user: hjibo
     * create_date: 2018-09-29 10:19
     * param :
     */
    @RequestMapping("/modSysRole")
    @LogMethod(logName = "修改角色")
    public Map modSysRole(HttpServletRequest request){
        try {
            SysRole sysRole = new SysRole();

            if(HUtil.isNotEmpty(request.getParameter("sr00"))) sysRole.setSr00(request.getParameter("sr00"));
            if(HUtil.isNotEmpty(request.getParameter("sr01"))) sysRole.setSr01(request.getParameter("sr01"));
            if(HUtil.isNotEmpty(request.getParameter("sr05"))) sysRole.setSr05(request.getParameter("sr05"));
            if(HUtil.isNotEmpty(request.getParameter("sr10"))) sysRole.setSr10(Integer.parseInt(request.getParameter("sr10")));

            this.sysRoleService.updateDataAndUpdateCache(sysRole);
            return mapResult(SUCCESS_CODE,SUCCESS_MSG,LONG_0L,null);
        } catch (Exception e) {
            log.info("修改角色信息失败！");
            e.printStackTrace();
        }
        return mapResult(ERROR_CODE,ERROR_MSG,LONG_0L,null);
    }

    /**
     * description: 新增
     * create_user: hjibo
     * create_date: 2018-09-29 10:19
     * param :
     */
    @RequestMapping("/addSysRoleMenu")
    @LogMethod(logName = "关联角色菜单")
    public Map addSysRoleMenu(HttpServletRequest request){
        try {
            SysRoleMenu sysRoleMenu = new SysRoleMenu();

            if(HUtil.isNotEmpty(request.getParameter("roleId"))) sysRoleMenu.setSrm01(request.getParameter("roleId"));
            if(HUtil.isNotEmpty(request.getParameter("menus"))) sysRoleMenu.setTemp2(request.getParameter("menus"));

            this.sysRoleMenuService.insertDataAndUpdateCache(sysRoleMenu);

            return mapResult(SUCCESS_CODE,SUCCESS_MSG,LONG_0L,null);
        } catch (Exception e) {
            log.info("保存角色-菜单信息失败！");
            e.printStackTrace();
        }
        return mapResult(ERROR_CODE,ERROR_MSG,LONG_0L,null);
    }

    /**
     * description: 根据角色编号获取菜单编号
     * create_user: hjibo
     * create_date: 2018-09-29 10:19
     * param :
     */
    @RequestMapping("/getMenuIdsByRoleId")
    public String getMenuIdsByRoleId(HttpServletRequest request){
        try {
            return sysCache.getMenuIdsByRoleId(request.getParameter("sr02"));
        } catch (Exception e) {
            log.info("获取菜单编号信息失败！");
            e.printStackTrace();
        }
        return "";
    }


}
